//
//  QYLocationPickerView.swift
//  QYLocationPicker
//
//  Created by Joggy on 16/5/4.
//  Copyright © 2016年 Joggy. All rights reserved.
//

import UIKit

class QYLocationPickerView: UIView {

    fileprivate let screenWidth = UIScreen.main.bounds.width
    fileprivate let screenHeight = UIScreen.main.bounds.height
    fileprivate let pickerViewHeight: CGFloat = 240
    
    var picker: UIPickerView!
    var city: String = "北京"
    fileprivate var blurView: UIVisualEffectView!
    var selectButton: UIButton!
    fileprivate var cancelButton: UIButton!
    
    fileprivate var data: NSArray!
    fileprivate var province: String = ""
    fileprivate var cities: NSArray!
    
    init () {
        super.init(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
        blurView = UIVisualEffectView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
        blurView.effect = UIBlurEffect(style: UIBlurEffectStyle.extraLight)
        blurView.alpha = 0
        self.addSubview(blurView)
        picker = UIPickerView(frame: CGRect(x: 0, y: screenHeight + 44, width: screenWidth, height: pickerViewHeight))
        picker.delegate = self
        picker.dataSource = self
        self.addSubview(picker)
        let path = Bundle.main.path(forResource: "ProvincesAndCities.plist", ofType: nil)
        data = NSArray(contentsOfFile: path!)
        cities = (data[0] as! NSDictionary)["Cities"] as! NSArray
        //button
        cancelButton = UIButton(type: UIButtonType.system)
        cancelButton.frame = CGRect(x: 0, y: screenHeight, width: 88, height: 44)
        cancelButton.setAttributedTitle(NSAttributedString(string: "取消", attributes: [NSForegroundColorAttributeName: UIColor(red: 0.29, green: 0.686, blue: 0.494, alpha: 1), NSFontAttributeName: UIFont.systemFont(ofSize: 18)]), for: UIControlState())
        cancelButton.addTarget(self, action: #selector(dismiss), for: UIControlEvents.touchUpInside)
        self.addSubview(cancelButton)
        selectButton = UIButton(type: UIButtonType.system)
        selectButton.frame = CGRect(x: screenWidth - 88, y: screenHeight, width: 88, height: 44)
        selectButton.setAttributedTitle(NSAttributedString(string: "确定", attributes: [NSForegroundColorAttributeName: UIColor(red: 0.29, green: 0.686, blue: 0.494, alpha: 1), NSFontAttributeName: UIFont.systemFont(ofSize: 18)]), for: UIControlState())
        self.addSubview(selectButton)
    }
    
    func show() {
        let keyWindow = UIApplication.shared.keyWindow
        keyWindow?.addSubview(self)
        UIView.animate(withDuration: 0.3, animations: {
            self.picker.frame.origin.y = self.screenHeight - self.pickerViewHeight
            self.blurView.alpha = 1
            self.cancelButton.frame.origin.y = self.screenHeight - self.pickerViewHeight - 44
            self.selectButton.frame.origin.y = self.screenHeight - self.pickerViewHeight - 44
        }) 
    }
    
    func dismiss() {
        UIView.animate(withDuration: 0.3, animations: { 
            self.picker.frame.origin.y = self.screenHeight + 44
            self.blurView.alpha = 0
            self.cancelButton.frame.origin.y = self.screenHeight
            self.selectButton.frame.origin.y = self.screenHeight
            }, completion: { (finish) in
                self.removeFromSuperview()
        }) 
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension QYLocationPickerView: UIPickerViewDelegate, UIPickerViewDataSource {
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return data.count
        }
        else {
            return cities.count
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 44
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return (data[row] as! NSDictionary)["State"] as? String
        }
        else {
            return (cities[row] as! NSDictionary)["city"] as? String
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {
            cities = (data[row] as! NSDictionary)["Cities"] as? NSArray
            pickerView.reloadComponent(1)
            city = (cities[pickerView.selectedRow(inComponent: 1)] as! NSDictionary)["city"] as! String
        }
        else {
            city = (cities[row] as! NSDictionary)["city"] as! String
        }
    }
    
}
